<template>
  <el-dialog title="分配角色" :visible.sync="showDialog">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="showDialog=false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/company'
import { allocateRole } from '@/api/employee'
import { getBasicInfo } from '@/api/user'
export default {
  data() {
    return {
      showDialog: false,
      roleList: [],
      roleIds: []
    }
  },
  methods: {
    // 打开弹窗
    async open(id) {
      // 获取员工列表
      const { rows } = await getRoleList({
        page: 1,
        pagesize: 9999
      })
      this.roleList = rows

      // 存储父组件传递过来的id
      this.employeeId = id
      // 获取员工角色回填
      const { roleIds } = await getBasicInfo(id)
      this.roleIds = roleIds
      // 显示弹窗
      this.showDialog = true
    },
    // 保存
    async save() {
      // 角色分配
      await allocateRole({
        id: this.employeeId,
        roleIds: this.roleIds
      })
      // 提示用户
      this.$message.success('保存成功')
      // 关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
